<?php
namespace Info\Controller;

use Info\Controller\CommonController;
use Common\Model\InfoContentModel;
use Common\Model\LinksModel;
use Common\Model\InfoCategoryModel;
use Common\Model\CommentModel;
class WapController extends CommonController
{

    public function index()
    {
        $pid = I('pid', '', 'int');
        $cid = I('cid', '', 'int');
        $area = I('area', '', 'int');
        $info = new InfoContentModel();
        $AllCategory = $this->getAllCategory();
        $infoListdata = $info->getInfoData($pid, $cid, $area);
        $categoryModel = new InfoCategoryModel;
        $category = $categoryModel->getCategory(1);
        $linksModel = new LinksModel();
        $links = $linksModel->getLinks();
        $pageShow = $this->PageShow($infoListdata['count']);
        $this->assign('data', $infoListdata['data'])
            ->assign('flinks', $links)
            ->assign('cid', $cid)
            ->assign('pid', $pid)
            ->assign('area', $area)
            ->assign('allCategory', $AllCategory)
            ->assign('pageShow', $pageShow)
            ->assign("category", $category);
        $this->display('/wap/index');
    }

    public function info_list(){
        $url =  'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
        $parameter = explode('-',pathinfo($url)['filename']);
        $pid = isset($parameter[0]) && (int)$parameter[0] >= 0 ? (int)$parameter[0] : 0;
        $cid = isset($parameter[1]) && (int)$parameter[1] >= 0 ? (int)$parameter[1] : 0;
        $area = isset($parameter[2]) && (int)$parameter[2] >= 0 ? (int)$parameter[2] : 0;
        $m1 = isset($parameter[3]) && (int)$parameter[3] > 0 ? (int)$parameter[3] : 0;
        $m2 = isset($parameter[4]) && (int)$parameter[4] > 0 ? (int)$parameter[4] : 0;

        $info = new InfoContentModel();
        $infoListdata = $info->getInfoData($pid,$cid,$area,$m1,$m2);
        $category = $this->getListCategory($pid);
        $categoryPid = array_column($category,'category','pid');
        $categoryCid = array_column($category,'category','cid');
        $AllCategory = $this->getAllCategory();

        $pageShow = $this->PageShow($infoListdata['count']);
        $this->assign('data',$infoListdata['data'])
            ->assign('cid',$cid)
            ->assign('pid',$pid)
            ->assign('area',$area)
            ->assign('m1',$m1)
            ->assign('m2',$m2)
            ->assign('categoryPid',$categoryPid)
            ->assign('categoryCid',$categoryCid)
            ->assign('allCategory',$AllCategory)
            ->assign('displaytype',$this->displaytype)
            ->assign('pageShow',$pageShow)
            ->assign("category",$category);

        $this->display('/wap/info_list');
    }

    public function info()
    {
        $id = I('id');
        if(!empty($id)){
            $data = $this->getInfoById($id);
            if(!empty($data)){
                //获取父级栏目id
                $cateogry = $this->getFatherCategory($data['cid']);
                //相关栏目
                $HotCategory = $this->getInfoCategory($data['cid'],$cateogry['father']['id']);
                //相关信息
                if(!empty($data['phone'])){
                    $data['phoneTimes'] = $this->getInfosByPhone($data['phone']);
                }else{
                    $data['phoneTimes'] = 0;
                }

                $commentModel = new CommentModel();
                $comment = $commentModel->getComByInfoId($id);

                $idArr = array_column($HotCategory,'id');
                $hotInfo = $this->getInfoInId($idArr);
                $area = C("city_area");
                $this->assign('data',$data)->assign('category',$HotCategory)
                    ->assign("hotInfo",$hotInfo)
                    ->assign("fatherCateogry",$cateogry['father'])
                    ->assign('currentCategory',$cateogry['current'])
                    ->assign('comment',$comment)
                    ->assign("area",$area);
                $this->display('/wap/info');
                exit();
            }else{
                //信息已过期或删除
                $this->redirect('/info/info/error404',array('id'=>$id));
            }
        }
        $this->redirect("/wap/info");
    }
}